package com.turing.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.turing.common.response.ResponseCode;
import com.turing.common.response.ResponseVO;
import com.turing.common.util.JwtUtil;
import com.turing.common.util.ResponseUtil;
import com.turing.user.dao.UserMapper;
import com.turing.user.entity.User;
import com.turing.user.service.UserService;
import com.turing.user.vo.UserVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtUtil jwtUtil;

    @Resource
    private UserMapper userMapper;

    /**
     * 登录接口
     * @param account 登录账号
     * @param password MD5加密后的密码
     * @return
     */
    @PostMapping("/login")
    public ResponseVO<Object> login(String account, String password){
        return userService.login(account, password);
    }

    @PostMapping("/linklogin")
    public ResponseVO<Object> linklogin(@RequestParam String account){
        return userService.skip2Login(account);
    }

    @PostMapping("/logout")
    public ResponseVO<Object> loginOut(){
        String token = request.getHeader("token");
        String result = userService.loginOut(token);
        if (ResponseCode.SUCCESS.equals(result)) {
            return ResponseUtil.success(result);
        }
        return ResponseUtil.error();
    }

    @PostMapping("changeMainRole")
    public ResponseVO<Object> changeMainRole(@RequestParam("roleId") Integer roleId){
        return ResponseUtil.success(userService.changeMainRole(roleId));
    }

    @GetMapping("CheckUserPrivilegeByUserId")
    public boolean CheckUserPrivilegeByUserId(@RequestParam("userId")Integer userId,@RequestParam("mainRoleId") Integer mainRoleId,@RequestParam("url")String url){
        return true;//userService.CheckUserPrivilegeByUserId(userId,mainRoleId,url);
    }

    @GetMapping("queryOne")
    public ResponseVO<Object> queryOne(@RequestParam("userId") Integer userId){
        return ResponseUtil.success(userService.queryOne(userId));
    }

    @GetMapping("getUserStatusById")
    public Integer getUserStatusById(@RequestParam("userId") Integer userId) {
        return userService.getUserStatusById(userId);
    }

    @GetMapping("/checkToken")
    public Boolean checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        try {
            Claims claims = jwtUtil.parseJWT(token);
            if(null != claims){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @GetMapping("/getUserListByName")
    List<User> getUserListByName(@RequestParam("name") String name) {
        return userMapper.selectList(new LambdaQueryWrapper<User>().like(User::getName, name));
    }

    @GetMapping("/getUserListByIds")
    List<User> getUserListByIds(@RequestParam("ids") List<Integer> ids) {
        return userMapper.selectBatchIds(ids);
    }
}
